//
//  MWCaptionView.m
//  MWPhotoBrowser
//
//  Created by Michael Waterfall on 30/12/2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MWCaptionView.h"
#import "MWPhoto.h"

//static const CGFloat labelPadding = 10;

// Private
@interface MWCaptionView () {
    id <MWPhoto> _photo;
    UITextView *_label;
}
@end

@implementation MWCaptionView

- (id)initWithPhoto:(id<MWPhoto>)photo {
    self = [super initWithFrame:CGRectMake(0, 0, 320, 44)]; // Random initial frame
    if (self) {
  //      self.userInteractionEnabled = NO;
        _photo = photo;
        if (MWPHOTO_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
            // Use iOS 7 blurry goodness
            self.barStyle = UIBarStyleBlackTranslucent;
        } else {
            // Transparent black with no gloss
            CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
            UIGraphicsBeginImageContext(rect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(context, [[UIColor colorWithWhite:0 alpha:0.6] CGColor]);
            CGContextFillRect(context, rect);
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            [self setBackgroundImage:image forToolbarPosition:UIBarPositionAny barMetrics:INTMAX_MAX];
        }
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
        [self setupCaption];
    }
    return self;
}

- (CGSize)sizeThatFits:(CGSize)size {
    float height = MaxHeight;
    if(_label.contentSize.height < MaxHeight)
    {
        if (MWPHOTO_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        {
            CGSize textSize = [_label.text sizeWithFont:_label.font
                                          constrainedToSize:CGSizeMake(size.width - labelPadding*2, MaxHeight) lineBreakMode:NSLineBreakByCharWrapping];
            height = textSize.height +  labelPadding * 4;
        }
        else
        {
            height = _label.contentSize.height;
        }
    }
    //    maxHeight = _label.font.lineHeight*maxNumberOfLine;
//    CGSize textSize = [_label.text sizeWithFont:_label.font
//                              constrainedToSize:CGSizeMake(size.width - labelPadding*2, maxHeight) lineBreakMode:NSLineBreakByCharWrapping
//                                  /*lineBreakMode:_label.lineBreakMode*/];
    return CGSizeMake(size.width, height/*textSize.height + labelPadding * 10*/);
}

- (void)setupCaption {
    _label = [[UITextView alloc] initWithFrame:CGRectIntegral(CGRectMake(labelPadding, 0,
                                                       self.bounds.size.width-labelPadding*2,
                                                       self.bounds.size.height))];
    _label.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    _label.opaque = NO;
    _label.backgroundColor = [UIColor clearColor];
    _label.textAlignment = UITextAlignmentCenter;
    _label.editable = NO;
//    _label.lineBreakMode = UILineBreakModeWordWrap;
//    _label.numberOfLines = 0;
    _label.textColor = [UIColor whiteColor];
//    if (SYSTEM_VERSION_LESS_THAN(@"7")) {
//        // Shadow on 6 and below
//        _label.shadowColor = [UIColor blackColor];
//        _label.shadowOffset = CGSizeMake(1, 1);
//    }
    _label.font = [_photo captionFont];
    if ([_photo respondsToSelector:@selector(caption)]) {
        _label.text = [_photo caption] ? [_photo caption] : @" ";
    }
    [self addSubview:_label];
}


@end
